package com.mashibing.juc.c_025;

import com.google.common.collect.Lists;
import com.google.common.util.concurrent.ForwardingBlockingDeque;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

public class T00_Deque {

    public static void main(String[] args) throws Exception {

        LinkedBlockingDeque linkedBlockingDeque = new LinkedBlockingDeque();

        new Thread(() -> {
            for (int i=0; i<100; i++) {
                try {
                    linkedBlockingDeque.putFirst(i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "A").start();

        new Thread(() -> {
            for (int i=0; i<101; i++) {
                try {
                    System.out.println(linkedBlockingDeque.takeLast());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "A").start();

        ForwardingBlockingDeque forwardingBlockingDeque = new ForwardingBlockingDeque() {
            @Override
            protected BlockingDeque delegate() {
                return linkedBlockingDeque;
            }
        };

        /*new Thread(() -> {
            for (int i=0; i<100; i++) {
                try {
                    forwardingBlockingDeque.putFirst(i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "C").start();*/

        forwardingBlockingDeque.putFirst(123);
        forwardingBlockingDeque.putLast(234);
        forwardingBlockingDeque.putLast(345);
        forwardingBlockingDeque.putLast(456);
//        System.out.println(forwardingBlockingDeque.removeFirstOccurrence(234));
        ArrayList<Object> arrayList = Lists.newArrayList();
        forwardingBlockingDeque.drainTo(arrayList);

        System.in.read();
    }

}
